package com.artbo.sporttema.components;

import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.BindingConstants;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.artbo.sporttema.entities.User;
import com.artbo.sporttema.pages.Index;
import com.artbo.sporttema.pages.Main;

/**
 * Layout component for pages of application sporttema.
 */
@IncludeStylesheet("context:layout/layout.css")
public class Layout
{
	@SessionAttribute
    private Boolean loggedIn;
	
    @Property
    private String pageName;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block leftpanel;
    
    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block mainpanel;

    @Inject
    private ComponentResources resources;

    public String getClassForPageName()
    {
      return resources.getPageName().equalsIgnoreCase(pageName)
             ? "current_page_item"
             : null;
    }

    public String[] getPageNames()
    {
      return new String[] { "Index", "About", "Contact" };
    }
    
    public boolean getLoggedIn(){
    	System.out.println("!!!!!!!!!!!!!"+loggedIn);
    	if(loggedIn==null)
    		return false;
    	else
    		return loggedIn;
    }
    
    @Property
	private String login;

	@Property
	private String password;
	
	
	@InjectPage
	private Main main;
	
	@InjectPage
	private Index index;
	
	@Inject
	private Session session;
	
	public Object onSubmitFromLoginForm() {
		Criteria criteria = session.createCriteria(User.class)
        .add(Restrictions.eq("login", login))
        .add(Restrictions.eq("password", password));
    if (!criteria.list().isEmpty()) {
    	loggedIn = true;
    	return main;
    }
    return index;
	}
}
